
package com.rt.schedulenew.utils.coder;
import java.security.MessageDigest;

public abstract class MDCoder extends SecurityCoder
{
    public static byte[] encodeMD2(final byte[] data) throws Exception {
        final MessageDigest md = MessageDigest.getInstance("MD2");
        return md.digest(data);
    }
    
    public static byte[] encodeMD4(final byte[] data) throws Exception {
        final MessageDigest md = MessageDigest.getInstance("MD4");
        return md.digest(data);
    }
    
    public static byte[] encodeMD5(final byte[] data) throws Exception {
        final MessageDigest md = MessageDigest.getInstance("MD5");
        return md.digest(data);
    }
    
    public static byte[] encodeTiger(final byte[] data) throws Exception {
        final MessageDigest md = MessageDigest.getInstance("Tiger");
        return md.digest(data);
    }
    
    public static String encodeTigerHex(final byte[] data) throws Exception {
        final byte[] b = encodeTiger(data);
        return new String(Hex.encode(b));
    }
    
    public static byte[] encodeWhirlpool(final byte[] data) throws Exception {
        final MessageDigest md = MessageDigest.getInstance("Whirlpool");
        return md.digest(data);
    }
    
    public static String encodeWhirlpoolHex(final byte[] data) throws Exception {
        final byte[] b = encodeWhirlpool(data);
        return new String(Hex.encode(b));
    }
    
    public static byte[] encodeGOST3411(final byte[] data) throws Exception {
        final MessageDigest md = MessageDigest.getInstance("GOST3411");
        return md.digest(data);
    }
    
    public static String encodeGOST3411Hex(final byte[] data) throws Exception {
        final byte[] b = encodeGOST3411(data);
        return new String(Hex.encode(b));
    }
    
    public static String MD5(final String data) throws Exception {
        final MessageDigest md = MessageDigest.getInstance("MD5");
        final byte[] array = md.digest(data.getBytes("UTF-8"));
        final StringBuilder sb = new StringBuilder();
        for (final byte item : array) {
            sb.append(Integer.toHexString((item & 0xFF) | 0x100).substring(1, 3));
        }
        return sb.toString().toUpperCase();
    }
}
